//
//  SearchBarView.m
//  CarTown
//
//  Created by xionghao on 15/8/11.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "SearchBarView.h"

@interface SearchBarView ()
@property (nonatomic,strong) CAShapeLayer *btnlayer;
@end

@implementation SearchBarView
+(instancetype)search{
    SearchBarView *search=[[[NSBundle mainBundle] loadNibNamed:@"SearchBarView" owner:nil options:nil]lastObject];
    return search;
}
- (IBAction)click:(UIButton *)sender {
    sender.selected=!sender.selected;
    [self rotate:sender];
}

-(instancetype)init{
    if (self=[super init]) {
        UIView *searchview=[[UIView alloc]initWithFrame:CGRectMake(16, 8, XHScreenwidth-32-46, 36)];
        [self addSubview:searchview];
        _searchbar=searchview;
        UIButton *choice=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 54, 36)];
        [choice setTitleColor:textnormalGary forState:UIControlStateNormal];
        [choice setTitle:@"车源" forState:UIControlStateNormal];
         choice.titleLabel.font=[UIFont systemFontOfSize:14];
        [searchview addSubview:choice];
        self.changchoice=choice;
        
        UITextField *textfield=[[UITextField alloc]initWithFrame:CGRectMake(60, 0, searchview.width-60, 36)];
        textfield.borderStyle=UITextBorderStyleNone;
        [searchview addSubview:textfield];
        searchview.backgroundColor=[UIColor whiteColor];
        self.searchtextField=textfield;
        [self addSubview:searchview];
        self.searchbar.layer.cornerRadius=3;
        self.btnlayer= [self createIndicatorWithColor:textnormalGary andPosition:CGPointMake(self.changchoice.width-5, self.changchoice.height/2)];
        [self.changchoice.layer addSublayer:self.btnlayer];
        self.frame=CGRectMake(0, 0, XHScreenwidth, 44);
        self.backgroundColor=bgGarycolor;
    }
    return self;
}

#pragma mark - animation method
- (void)animateIndicator:(CAShapeLayer *)indicator Forward:(BOOL)forward {
    [CATransaction begin];
    [CATransaction setAnimationDuration:0.25];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithControlPoints:0.4 :0.0 :0.2 :1.0]];
    
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    anim.values = forward ? @[ @0, @(M_PI) ] : @[ @(M_PI), @0 ];
    
    if (!anim.removedOnCompletion) {
        [indicator addAnimation:anim forKey:anim.keyPath];
    } else {
        [indicator addAnimation:anim forKey:anim.keyPath];
        [indicator setValue:anim.values.lastObject forKeyPath:anim.keyPath];
    }
    
    [CATransaction commit];
    
    
}

- (CAShapeLayer *)createIndicatorWithColor:(UIColor *)color andPosition:(CGPoint)point {
    CAShapeLayer *layer = [CAShapeLayer new];
    
    UIBezierPath *path = [UIBezierPath new];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(8, 0)];
    [path addLineToPoint:CGPointMake(4, 5)];
    [path closePath];
    
    layer.path = path.CGPath;
    layer.lineWidth = 1.0;
    layer.fillColor = color.CGColor;
    
    CGPathRef bound = CGPathCreateCopyByStrokingPath(layer.path, nil, layer.lineWidth, kCGLineCapButt, kCGLineJoinMiter, layer.miterLimit);
    layer.bounds = CGPathGetBoundingBox(bound);
    
    layer.position = point;
    
    return layer;
}



- (void)rotate:(UIButton *)sender {
    
  [self animateIndicator:self.btnlayer Forward:sender.selected];
        
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)awakeFromNib{
    self.height=44;
    self.width=XHScreenwidth;
    self.searchbar.layer.cornerRadius=3;
   self.btnlayer= [self createIndicatorWithColor:textnormalGary andPosition:CGPointMake(self.changchoice.width-5, self.changchoice.height/2)];
    [self.changchoice.layer addSublayer:self.btnlayer];
}
@end
